/**
 * 
 */
package uy.com.pzit.fpmundialmodel.dao.dbObjects;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.jdbc.Work;

/**
 * @author pferraz
 * 
 */
public class DBObject {
	private static Logger logger = Logger.getLogger(DBObject.class);

	public DBObject() {
	}

	public static boolean addObject(Object o) {
		Session sess = null;
		Transaction tx = null;
		try {
			sess = DBTemplate.getConnTemplate().getSessionFactory()
					.openSession();
			tx = sess.beginTransaction();
			sess.save(o);
			tx.commit();
		} catch (HibernateException e) {
			logger.error("Error al persistir objeto " + o.getClass(), e);
			tx.rollback();
			return false;
		} finally {
			try {
				if (sess != null) {
					sess.close();
				}
			} catch (Exception e) {
				logger.error(e);
			}
		}
		return true;
	}

	public static boolean updateObject(Object o) {
		Session sess = null;
		Transaction tx = null;
		try {
			sess = DBTemplate.getConnTemplate().getSessionFactory()
					.openSession();
			tx = sess.beginTransaction();
			sess.update(o);
			tx.commit();
			return true;
		} catch (HibernateException e) {
			logger.error("Error al actualizar " + o.getClass(), e);
			tx.rollback();
			return false;
		} finally {
			if (sess != null) {
				sess.close();
			}

		}
	}

	public static <T> Object getObjectById(Class<T> oClass, Object id) {
		Session sess = null;
		try {
			sess = DBTemplate.getConnTemplate().getSessionFactory()
					.openSession();
			sess.beginTransaction();

			return sess.get(oClass, (Serializable) id);
		} catch (HibernateException e) {
			logger.error("Error al consultar", e);
			return false;
		} finally {
			if (sess != null) {
				sess.close();
			}

		}
	}

	public static boolean removeObject(Object o) {
		Session sess = null;
		Transaction tx = null;
		try {
			sess = DBTemplate.getConnTemplate().getSessionFactory()
					.openSession();
			tx = sess.beginTransaction();
			sess.delete(o);
			tx.commit();
			return true;
		} catch (HibernateException e) {
			logger.error("Error al elminar dato", e);
			tx.rollback();
			return false;
		} finally {
			if (sess != null) {
				sess.close();
			}
		}
	}

	public static boolean txInsertUpdateDelete(String sqlQuery) {
		Session session = DBTemplate.getConnTemplate().getSessionFactory()
				.openSession();
		Transaction transaction = null;
		try {

			transaction = session.beginTransaction();
			Query query = session.createSQLQuery(sqlQuery);
			query.executeUpdate();
			session.getTransaction().commit();
			session.flush();
			return true;
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return false;

	}

	public static void executePureQuery(final String[] query) {
		DBTemplate.getConnTemplate().getSessionFactory().openSession()
				.doWork(new Work() {
					public void execute(Connection connection)
							throws SQLException {
						Statement statement = null;
						try {
							statement = connection.createStatement();

							// execute update SQL stetement
							for (int i = 0; i < query.length; i++) {
								statement.execute(query[i]);
							}

							connection.commit();
							logger.debug("commited "+query);

						} catch (SQLException e) {

							logger.error(e.getMessage());

						} finally {

							if (statement != null) {
								statement.close();
							}

							if (connection != null) {
								connection.close();

							}

						}
					}
				});

	}
}
